package com.kongge.viewdisplayframeworklib.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kongge.viewdisplayframeworklib.R;
import com.kongge.viewdisplayframeworklib.control.PublicInterface;
import com.kongge.viewdisplayframeworklib.model.NavigateItem;

public class NavigateBar extends RelativeLayout {

	private ViewGroup mLeftViewGroup;
	private ViewGroup mMiddleViewGroup;
	private ViewGroup mRightViewGroup;

	private ImageView mBackButton;
	private TextView mTitleView;

	public NavigateBar(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}

	public NavigateBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void refresh(NavigateItem item) {

		mLeftViewGroup.removeAllViews();
		mMiddleViewGroup.removeAllViews();
		mRightViewGroup.removeAllViews();
		if (item == null || item.getLeftView() == null) {
			
			
			if (PublicInterface.isCanGoBack()) {

				if (mBackButton == null) {
					mBackButton = new ImageView(getContext());
					mBackButton.setScaleType(ImageView.ScaleType.FIT_XY);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
							getResources().getDimensionPixelOffset(R.dimen.navigate_bar_back_width),
							getResources().getDimensionPixelOffset(R.dimen.navigate_bar_back_height));
					mBackButton.setLayoutParams(params);
					Bitmap weakBitmap = BitmapFactory
							.decodeResource(getContext().getResources(),
									R.drawable.back);
					mBackButton.setImageBitmap(weakBitmap);
					mBackButton.setBackgroundResource(R.drawable.back_bg);
					mBackButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							PublicInterface.goBack();
						}
					});
				}
				if (mBackButton != null) {
					mLeftViewGroup.addView(mBackButton);
				}
			}
		} else {
			mLeftViewGroup.addView(item.getLeftView());
		}
		if (item == null || item.getMiddleView() == null) {
			if (mTitleView == null) {
				mTitleView = new TextView(getContext());
				mTitleView.setGravity(Gravity.CENTER_VERTICAL
						| Gravity.CENTER_HORIZONTAL);
				mTitleView.setTextColor(getResources().getColor(R.color.navigate_bar_title_textcolor));
				mTitleView.setTextSize(getResources().getDimensionPixelOffset(R.dimen.navigate_bar_title_textsize));
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
				mTitleView.setLayoutParams(params);
			}
			if (mTitleView != null) {
				mTitleView.setText(PublicInterface.getCurPageTitle());
				mMiddleViewGroup.addView(mTitleView);
			}
		} else {
			mMiddleViewGroup.addView(item.getMiddleView());
		}
		
		if (item == null || item.getRightView() == null) {
			
		} else {
			mRightViewGroup.addView(item.getRightView());
		}
		

	}

	protected void onFinishInflate() {
		super.onFinishInflate();
		mLeftViewGroup = (ViewGroup) findViewById(R.id.title_bar_left_container);
		mMiddleViewGroup = (ViewGroup) findViewById(R.id.title_bar_middle_container);
		mRightViewGroup = (ViewGroup) findViewById(R.id.title_bar_right_container);
	}

}
